<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class VarietiesController extends Controller
{
    //
    //首页
    public function getIndex()
    {
        $first = DB::table('varieties') -> select(DB::raw('first')) -> groupBy('first') -> get();
        // dd($first);
        //将first转换为数组
        foreach($first as $k => $v){
            $dd[] = $v -> first;
        }
        // dd($dd);
        //将所有的品名压入到数组中
        foreach($dd as $k => $v){
            $name[] = DB::table('varieties') -> select(DB::raw('name')) -> where('first',$v) -> get();
        }
        // dd($name);
        $size = '';
        //改为关联数组,下标是首字母
        foreach($dd as $k=>$v){
            for($t=0;$t<count($name[$k]);$t++){
                $size++;
                $data[$v][$t]=$name[$k][$t]->name;
            }
        }
        // dd($size);
        $py = DB::table('varieties') -> select(DB::raw('pinyin')) -> where('first','h') -> groupBy('name') -> get();
        // dd($data);

        //属性数组
        $sx = array('易产假','易储存 ','易走色变油','易虫易霉','易受疫情影响','易受倒春寒影响','易受旱情影响','易受雨涝影响','生长期3年以上','生长期1-3年','生长期1年','生长期易受行情影响','进口','毒性','贵细','种植费工时','采挖加工费工时','常用大宗','基药包含','小量冷背','野生主供','家种主供','香料类','滋补类','汤料类','茶饮类 ','酒料类');

    	return view('Home.varieties.index',['first' => $first,'name' => $data,'size' => $size,'sx' => $sx]);
    }

    //获取名字
    public function getList($name)
    {
        //======获取数据
        $res = DB::table('varieties') -> where('name',$name) -> groupBy('name') -> get();
        // dd($res);
        //======获取该品种的价格信息
        $price = DB::table('mprice') -> where('mName',$name) -> get();
        // dd($price);
        //======获取该品种的电商价格
        $store = DB::table('medicinal') -> where('name',$name) -> get();
        // dd($store);
        //======获取该品种的产地快讯
        $fid = DB::table('portaltype') -> select('id') -> where('name','产地快讯') -> first();
        $from = DB::table('portalpost') -> where('tid',$fid -> id) -> where('mName',$name) -> orderBy('ctime','asc')-> skip(0) -> take(5)  -> get();
        // dd($from);
        //======获取市场快讯
        $mid = DB::table('portaltype') -> select('id') -> where('name','市场快讯') -> first();
        $market = DB::table('portalpost') -> where('tid',$mid -> id) -> where('mName',$name) -> orderBy('ctime','asc')-> skip(0) -> take(5)  -> get();
        // dd($market);
        //=====获取品种分析信息
        $vid = DB::table('portaltype') -> select('id') -> where('name','品种分析') -> first();
        $varie = DB::table('portalpost') -> where('tid',$vid -> id) -> where('mName',$name) -> orderBy('ctime','asc')-> skip(0) -> take(5)  -> get();
        
        //=====获取相关内容信息
        // $iid = DB::table('portaltype') -> select('id') -> where('name','品种分析') -> first();
        $info = DB::table('portalpost') 
            -> join('portaltype','portalpost.tid','=','portaltype.id')
            -> select('portalpost.id','portalpost.title','portaltype.id as tid','portaltype.name')
            -> where('title','like','%'.$name.'%') 
            -> orderBy('ctime','desc') 
            -> skip(0) -> take(6) -> get();
        // dd($info);
        return view('Home.varieties.list',['res' => $res,'price' => $price,'store' => $store,'from' => $from,'market' => $market,'varie' => $varie,'info' => $info]);

    }

    //品种详情页
    public function getDetial($name)
    {  
        // dd($name);
    	//获取品种信息
        $vname = DB::table('varieties') -> where('name',$name) ->get();
        //获取详情
    	$res = DB::table('bookdetial') -> where('mName',$name) -> get();
    	
    	// dd($res);
    	return view('Home.varieties.detial',['name' => $vname , 'varieties' => $res]);
    }

    //获取首字母的ajax请求
    public function postAjax(Request $request)
    {
        // dd($request -> pinyin);
        $first = $request -> pinyin;
        $pinyin = DB::table('varieties') -> select('pinyin') -> where('pinyin','like',$first.'%') -> groupBy('pinyin') ->get();
        // $pinyin = $py[0];
        // dd($pinyin);
        $name = DB::table('varieties') -> select('name') -> where('first',$first) ->get();
        // $name = DB::table('varieties') -> select
        $data[] = $pinyin;
        $data[] = $name;
        // dd($data);
        echo json_encode($data);
    }

    // 获取拼音的ajax请求
    public function postName(Request $request)
    {
        $py = $request -> name;
        $name = DB::table('varieties') -> select('name') 
            -> where('pinyin','like',$py.'%') 
            -> get();
        // dd($name);
        echo json_encode($name);
    }

    //药材属性的ajax请求
    public function postProperties(Request $request)
    {
        $sx = trim($request -> sx);
        // dd($sx);
        $name = DB::table('varieties') -> select('name') -> where('properties','like','%'.$sx.'%') -> get();
        // dd($name);
       echo json_encode($name);
    }
}
